<template>
  <div class="punish-dict-list">
    <a-card title="处分字典管理">
      <template #extra>
        <a-button type="primary" @click="showAddModal">添加处分字典</a-button>
      </template>

      <a-table 
        :columns="columns" 
        :data-source="dataSource" 
        :loading="loading"
        bordered 
        :pagination="pagination"
      >
        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'action'">
            <a-space>
              <a @click="showEditModal(record)">编辑</a>
              <a style="color: #ff4d4f" @click="handleDelete(record.id)">删除</a>
            </a-space>
          </template>
        </template>
      </a-table>
    </a-card>

    <a-modal 
      v-model:open="modalVisible" 
      :title="modalTitle" 
      @ok="handleSubmit" 
      @cancel="handleCancel"
      width="600px"
    >
      <a-form :model="formData" :label-col="{ span: 6 }" :wrapper-col="{ span: 16 }">
        <a-form-item label="处分类别" required>
          <a-input v-model:value="formData.sort" placeholder="请输入" />
        </a-form-item>
        <a-form-item label="处分名称" required>
          <a-input v-model:value="formData.name" placeholder="请输入" />
        </a-form-item>
        <a-form-item label="处分级别" required>
          <a-input v-model:value="formData.ranks" placeholder="请输入" />
        </a-form-item>
        <a-form-item label="扣分数" required>
          <a-input-number 
            v-model:value="formData.grade" 
            placeholder="请输入" 
            :min="0"
            style="width: 100%"
          />
        </a-form-item>
      </a-form>
    </a-modal>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { message, Modal } from 'ant-design-vue'
import { punishDictAPI } from '@/api/system'
import type { PunishDict } from '@/api/system'

const loading = ref(false)
const modalVisible = ref(false)
const modalTitle = ref('添加处分字典')
const currentRecord = ref<PunishDict | null>(null)
const dataSource = ref<PunishDict[]>([])

const formData = ref({
  sort: '',
  name: '',
  ranks: '',
  grade: undefined as number | undefined
})

const columns = [
  { title: 'ID', dataIndex: 'id', key: 'id', width: 70 },
  { title: '处分类别', dataIndex: 'sort', key: 'sort', width: 150 },
  { title: '处分名称', dataIndex: 'name', key: 'name' },
  { title: '级别', dataIndex: 'ranks', key: 'ranks', width: 120 },
  { title: '扣分数', dataIndex: 'grade', key: 'grade', width: 100 },
  { title: '操作', key: 'action', width: 150 }
]

const pagination = ref({
  total: 0,
  current: 1,
  pageSize: 10,
  showTotal: (total: number) => `共 ${total} 条`,
  onChange: (page: number, pageSize: number) => {
    pagination.value.current = page
    pagination.value.pageSize = pageSize
    loadData()
  }
})

// 加载数据
const loadData = async () => {
  loading.value = true
  try {
    const res = await punishDictAPI.getList()
    dataSource.value = res.data.results
    pagination.value.total = res.data.count
  } catch (error: any) {
    message.error('加载数据失败: ' + (error.response?.data?.message || error.message))
  } finally {
    loading.value = false
  }
}

// 显示添加弹窗
const showAddModal = () => {
  modalTitle.value = '添加处分字典'
  currentRecord.value = null
  formData.value = {
    sort: '',
    name: '',
    ranks: '',
    grade: undefined
  }
  modalVisible.value = true
}

// 显示编辑弹窗
const showEditModal = (record: PunishDict) => {
  modalTitle.value = '编辑处分字典'
  currentRecord.value = record
  formData.value = {
    sort: record.sort,
    name: record.name,
    ranks: record.ranks,
    grade: record.grade
  }
  modalVisible.value = true
}

// 提交
const handleSubmit = async () => {
  if (!formData.value.sort || !formData.value.name || !formData.value.ranks) {
    message.error('请填写完整的处分信息')
    return
  }
  if (formData.value.grade === undefined || formData.value.grade < 0) {
    message.error('请输入有效的扣分数')
    return
  }

  try {
    if (currentRecord.value) {
      // 编辑
      await punishDictAPI.update(currentRecord.value.id, formData.value as any)
      message.success('更新成功')
    } else {
      // 新增
      await punishDictAPI.create(formData.value as any)
      message.success('添加成功')
    }
    modalVisible.value = false
    loadData()
  } catch (error: any) {
    message.error('操作失败: ' + (error.response?.data?.message || error.message))
  }
}

// 取消
const handleCancel = () => {
  modalVisible.value = false
}

// 删除
const handleDelete = (id: number) => {
  Modal.confirm({
    title: '确认删除',
    content: '确定要删除这条处分字典吗？',
    okText: '确定',
    okType: 'danger',
    cancelText: '取消',
    onOk: async () => {
      try {
        await punishDictAPI.delete(id)
        message.success('删除成功')
        loadData()
      } catch (error: any) {
        message.error('删除失败: ' + (error.response?.data?.message || error.message))
      }
    }
  })
}

onMounted(() => {
  loadData()
})
</script>

<style scoped>
</style>
